﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using eService.BLL;
using HLVControl.Grid;
using HLVControl.Grid.Data;
using APP.QUANLY.Common;

namespace eService.Presentation
{
    public partial class usrcDanhMuc_DoiTuong : UserControl
    {
        EnumNameForm.ThaoTac thaotac;
        ExcuteDOITUONG exe;
        ExcuteKHUVUC exeKV;

        public usrcDanhMuc_DoiTuong()
        {
            InitializeComponent();
            LoadData();
        }
        private void usrcDanhMuc_DoiTuong_Load(object sender, EventArgs e)
        {
            LoadAll();
        }

        protected virtual void LoadAll()
        {
            LoadConnection();
            LoadData();
            LoadUI();
        }
        private void LoadConnection()
        {
            ConnectionProvider.LoadAllConnection();
        }
        private void LoadUI()
        {
            exe = new ExcuteDOITUONG();
            exeKV = new ExcuteKHUVUC();
            thaotac = EnumNameForm.ThaoTac.macdinh;
            ControlUI(thaotac);
            UIHelper.LoadFocus(txtTimKiem);
        }
        public void LoadData()
        {
            LoadCmbDKhuVuc();
        }
        private void LoadCmbDKhuVuc()
        {
            try
            {
                List<GROUPROOM> list = exeKV.GetAll();
                DataTable data = new DataTable();
                data.Columns.Add("Mã", typeof(int));
                data.Columns.Add("Khu vực", typeof(string));
                foreach (GROUPROOM obj in list)
                {
                    data.Rows.Add(obj.GrID, obj.GrName);
                }
                cmbKhuVuc.DataSource = data;
                cmbKhuVuc.DisplayMember = "Khu vực";
                cmbKhuVuc.ValueMember = "Mã";
                cmbKhuVuc.ColumnWidths = "0,200";
                cmbKhuVuc.SelectedIndex = -1;
                cmbKhuVuc.Text = "";
            }
            catch
            {

            }
        }

        private void ControlUI(EnumNameForm.ThaoTac thaotac)
        {
            if (thaotac == EnumNameForm.ThaoTac.them || thaotac == EnumNameForm.ThaoTac.sua)
            {
                SetStateButton(false);
                if (thaotac == EnumNameForm.ThaoTac.them)
                    LoadValuesToUI(null);
                SetTextReadOnly(false);
                UIHelper.LoadFocus(txtTen);
            }
            if (thaotac == EnumNameForm.ThaoTac.macdinh || thaotac == EnumNameForm.ThaoTac.xoa)
            {
                SetStateButton(true);
                LoadValuesToUI(null);
                SetTextReadOnly(true);

                //Load TreeView
                FillTreeView();
                treeview.RefreshGrouping();

                txtTimKiem.Text = "";
                UIHelper.LoadFocus(txtTimKiem);
            }
        }
        private void SetStateButton(bool IsEnabled)
        {
            btnSua.Enabled = IsEnabled;
            btnThem.Enabled = IsEnabled;
            btnXoa.Enabled = IsEnabled;
            btnXacNhan.Visible = !IsEnabled;
            btnHuy.Visible = !IsEnabled;
            treeview.Enabled = IsEnabled;
            btnTimKiem.Enabled = IsEnabled;
        }
        private void SetTextReadOnly(bool IsReadOnly)
        {
            txtTen.ReadOnly = IsReadOnly;
            txtGhiChu.ReadOnly = IsReadOnly;
            numSTT.Enabled = !IsReadOnly;
            numTu0Den8.Enabled = !IsReadOnly;
            numTu18Den24.Enabled = !IsReadOnly;
            numTu8Den18.Enabled = !IsReadOnly;
            cmbKhuVuc.Enabled = !IsReadOnly;
        }
        private void LoadValuesToUI(ROOM obj)
        {
            if (obj == null)
            {
                obj = new ROOM();
                obj.RID=0;
                obj.GrID=0;
                obj.Poss = exe.GetAll().Count + 1;
                obj.Price5 = 0;
                obj.Price4 = 0;
                obj.Price3 = 0;
                obj.Price2 = 0;
                obj.RPrice = 0;
                
            }
            labMa.Text = obj.RID.ToString();
            txtTen.Text = obj.RName;
            numTu0Den8.Value = (Decimal)obj.RPrice;
            numTu8Den18.Value = (Decimal)obj.Price2;
            numTu18Den24.Value = (Decimal)obj.Price3;
            numSTT.Value = (int)obj.Poss;
            txtGhiChu.Text = obj.RDes;
            labMaKhuVuc.Text = obj.GrID.ToString();
            GROUPROOM objP = exeKV.GetByID((int)obj.GrID);
            if (objP == null) return;
            cmbKhuVuc.Text = objP.GrName;
        }
        private void FillTreeView()
        {
            List<ROOM> data = exe.GetAll();
            exe.LoadToTreeview(treeview, data);
        }
        private bool XuLy(EnumNameForm.ThaoTac thaotac)
        {
            var flag = false;
            try
            {
                ROOM obj = exe.LoadValues(int.Parse(labMa.Text),int.Parse(labMaKhuVuc.Text), txtTen.Text,(double)numTu0Den8.Value,(double)numTu8Den18.Value,(double)numTu18Den24.Value,0,0,(int)numSTT.Value, txtGhiChu.Text);
                flag = exe.KQ;
                if (flag)
                {
                    flag = false;
                    switch (thaotac)
                    {
                        case EnumNameForm.ThaoTac.them:
                            flag = exe.Add(obj);
                            break;
                        case EnumNameForm.ThaoTac.sua:
                            flag = exe.UpDate(obj);
                            break;
                        case EnumNameForm.ThaoTac.xoa:
                            flag = exe.Delete(obj);
                            break;
                    }
                    flag = exe.KQ;
                }
                MessageBox.Show(exe.ErrMsg, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                flag = false;
            }
            return flag;
        }
        private void button_Click(object sender, EventArgs e)
        {
            bool KQ = false;
            Button btn = (Button)sender;
            string _thaotac = btn.Tag.ToString();
            if ((_thaotac == "sua" && labMa.Text == "0") || (_thaotac == "xoa" && labMa.Text == "0"))
            {
                MessageBox.Show("Vui lòng chọn đối tượng!");
                return;
            }
            switch (_thaotac)
            {
                case "them":
                    thaotac = EnumNameForm.ThaoTac.them;
                    break;
                case "sua":
                    thaotac = EnumNameForm.ThaoTac.sua;
                    break;
                case "huy":
                    thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
                case "xoa":
                    KQ = XuLy(EnumNameForm.ThaoTac.xoa);
                    if (KQ) thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
                case "xacnhan":
                    KQ = XuLy(thaotac);
                    if (KQ) thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
            }
            ControlUI(thaotac);
        }
        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            SearchProvider.SmartSearch(treeview, txtTimKiem.Text.Trim(), new List<string> {});
        }

        private void treeview_AfterSelectionChange(object sender, HLVControl.Grid.Events.TreeListElementEventArgs e)
        {
            try
            {
                TreeListRow firstSelected = treeview.SelectedElements[0] as TreeListRow;
                int ID = int.Parse(firstSelected.Cells["colID"].Value.ToString());
                ROOM obj = exe.GetByID(ID);
                if (obj == null) return;
                LoadValuesToUI(obj);
            }
            catch
            {

            }
        }
        private void txt_Enter(object sender, EventArgs e)
        {
            UIHelper.txt_Enter(sender, e);
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            UIHelper.txt_Leave(sender, e);
        }
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                TextBox txt = sender as TextBox;
                if (txt.Name == txtTen.Name)
                    UIHelper.txt_KeyPress(sender, numTu0Den8, e);
                if (txt.Name == numTu0Den8.Name)
                    UIHelper.txt_KeyPress(sender, numTu8Den18, e);
                if (txt.Name == numTu8Den18.Name)
                    UIHelper.txt_KeyPress(sender, numTu18Den24, e);
                if (txt.Name == numTu18Den24.Name)
                    UIHelper.txt_KeyPress(sender, cmbKhuVuc, e);
                if (txt.Name == cmbKhuVuc.Name)
                    UIHelper.txt_KeyPress(sender, numSTT, e);
                if (txt.Name == numSTT.Name)
                    UIHelper.txt_KeyPress(sender, txtGhiChu, e);
                if (txt.Name == txtGhiChu.Name)
                    UIHelper.txt_KeyPress(sender, btnXacNhan, e);
                if (txt.Name == txtTimKiem.Name)
                    UIHelper.txt_KeyPress(sender, btnTimKiem, e);
            }
            catch { }
        }

        private void cmbKhuVuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbKhuVuc.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbKhuVuc.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labMaKhuVuc.Text = Ma;
                cmbKhuVuc.Text = Ten;
            }
        }
    }
}
